import { EnumplatFrom } from "../../../../Core/Sdk/EnumplatFrom";
import SdkTool from "../../../../Core/Sdk/SdkTool";
import ModuleWindow from "../../window/ModuleWindow";
import AbstractScene from "../AbstractScene";
import * as fgui from "fairygui-cc";
import WindowObject from "../../window/WindowObject";
import ConfigManager from "../../../Config/ConfigManager";
import { LocalizationManager } from "../../../../Core/Language/LocalizationManager";

export class SceneLogin extends AbstractScene {
    protected init(): void {
        super.init();
    }
    public enterScene(): void {
        console.log("进入登录场景");
        this.onSceneEntered();
        //加载通用资源包
        fgui.UIPackage.loadPackage("UI/Common", (err) => {
            //加载主界面资源包
            fgui.UIPackage.loadPackage("UI/Font", (err) => {
                fgui.UIPackage.loadPackage("UI/Start", this.onUILoaded.bind(this));
            });
        });
    }
    onUILoaded() {
        switch (SdkTool.ins.platform) {
            case EnumplatFrom.TT:
                ModuleWindow.ins.openView(WindowObject.ins.TtStartView);
                break;
            case EnumplatFrom.WX:
                ModuleWindow.ins.openView(WindowObject.ins.WxStartView);
                break;
            default:
                ModuleWindow.ins.openView(WindowObject.ins.BaseStartView);
                break;
        }
        // if (this.replaceText()) {
        //     console.log("本地化文件替换成功")
        // }

    }
    exitScene(): void {
        console.log("退出登录场景");
        ModuleWindow.ins.closeAllWindows(true);
        fgui.UIPackage.removePackage("Start");
        this.onSceneExited();
    }
    /**替换文本 */
    private replaceText(): boolean {
        if (!ConfigManager.tables || !LocalizationManager.ins.curLanguageDesc) {
            console.log(`config or language is null`)
            return false;
        }
        if (ConfigManager.tables) {
            for (let i in ConfigManager.tables) {
                for (let j of ConfigManager.tables[i].getDataList()) {
                    for (let k in j) {
                        if (typeof j[k] === 'string' && j[k].indexOf('LANGUAGES_') > -1) {
                            j[k] = LocalizationManager.ins.getLocalizedString(LocalizationManager.ins.currentLanguage + j[k].splice("_")[1]);
                        }
                    }
                }

            }
        }
        return true;
    }
} 